#include<iostream>
#include<cstdio>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int sz[N];
int p[N];
LL d[N];
int n, m;
int find(int x)
{
	if (p[x] != x) {
		int root = find(p[x]);
		d[x] += d[p[x]];
		p[x] = root;
	}
	return p[x];
}
int main()
{
	while (cin >> n >> m) {
		for (int i = 0; i <= n; i++) {
			p[i] = i;
			d[i] = 0;
		}
		int ans = 0;
		while (m--) {
			int l, r;
			LL z;
			cin >> l >> r >> z;
			int py = find(r);
			int px = find(l - 1);
			if (px == py) {
				LL sum = d[l - 1] - d[r];
				if (sum != z)ans++;
			}
			else {
				p[px] = py;
				d[px] = z + d[r] - d[l - 1];
			}
		}
		cout << ans << endl;
	}
	return 0;
}